/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nawma77
 */
public class SSN
{

public SSN(String ssn)
 {
 ssnControl(ssn);
 }

private boolean c_ssnOK;
private String c_ssn;

public boolean isOK()
{
return c_ssnOK;
}

public boolean isMale()
{
return !isFemale();
}

public boolean isFemale()
{
if(c_ssnOK)
  {
  int siffra = Character.digit(c_ssn.charAt(9),10);
  if(siffra % 2 == 0)
      return true;
  else
      return false;
  }
return false;
}

public String toString()
{
if(c_ssnOK)
    return c_ssn;
else
    return "";
}

private void ssnControl(String s)
  {
    char c;
	if(s !=null && (!s.equals("")))
		{
        int length = s.length();
		if((length == 10) || (length == 11))
		{
        if(s.length() == 10)
         {
         for(int i=0; i< s.length();i++)
            {
            c = s.charAt(i);
            if(!Character.isDigit(c))
              {
              c_ssnOK = false;
              return;
              }
            }
         checkControlNumber(s);
         return;
        }// END LENGD 10
        // Om lengden är 11
        else
         {
          // Se om bind sträkan är på rätt plats
         c = s.charAt(6);
         if(c != '-')
            {
            c_ssnOK = false;
            return;
            }
          else
           {
           // En string utan sträka
           String temp = s.substring(0,6)+s.substring(7);
           checkControlNumber(temp);
            return;
           }
         } // END IF LENGTH = 11

        } // END IF LENGTH 11 AND 10
        else
        {
        c_ssnOK = false;
        return;
        }
      } // IF IS NOT EMPTY STRING
  }// END OF METHOD
private void checkControlNumber(String s)
{
int siffra;
char c;
int summa = 0;
for(int i=0; i< s.length()-1;i++)
   {
   c = s.charAt(i);
   siffra =Character.digit(c, 10);
   if(i % 2 == 0)
    {
    siffra = siffra *2;
    if(siffra >= 10)
     {
     int rest = siffra%10;
     siffra = rest + siffra/10;
     }
    summa = summa + siffra;
    }
   else
      summa = summa + siffra;
   }
   int controlsiffra = summa %10;
   controlsiffra = 10- controlsiffra;
   if (controlsiffra == 10)
       controlsiffra = 0;
   c = s.charAt(9);
   if(Character.digit(c, 10)== controlsiffra)
     {
     c_ssnOK = true;
     c_ssn = s.substring(0,6)+"-"+s.substring(6);
     }
   else
     c_ssnOK = false;
}

}